﻿Imports System
Imports System.Text
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports D2L.Extensibility.AuthSdk
Imports D2L.Extensibility.AuthSdk.Restsharp
Imports System.Reflection

Namespace Authenticate

    Public Class Authenticate

#Region "Environment Attributes"

        Protected Const APP_ID As String = "G9nUpvbZQyiPrk3um2YAkQ"
        Protected Const APP_KEY As String = "ybZu7fm_JKJTFwKEHfoZ7Q"

        Protected Const USER_ID As String = "KOOxZJijDZ3k7cnre-fHFK"
        Protected Const USER_KEY As String = "bCdB88XbvWwpeL6JAQVWNF"

        Protected Const LMS_PORT As Integer = 443
        Protected Const LMS_HOST As String = "lms.valence.desire2learn.com"
        Protected Const LMS_HOST_HTTPS As String = "https://lms.valence.desire2learn.com"
        Protected Const SCHEME As String = "https"

        Public Const LP_VERSION As String = "1.2"
        Public Const ORG_UNIT = "6606"
        Public Const LMS_GRADE_OBJ_ID = ""
        Public Const USER_ID_GRADE = "1"

#End Region

#Region "Common Objects"

        Private _appFactory As D2LAppContextFactory = New D2LAppContextFactory()
        Private _appContext As ID2LAppContext

#End Region

        Public Sub New()
            _appContext = _appFactory.Create(Authenticate.APP_ID, Authenticate.APP_KEY)
        End Sub

        Public Function ExecuteService_with_UserContext(ByVal serviceToCall As String, ByVal RestMethod As RestSharp.Method, ByVal TypeObj As Type) As RestSharp.IRestResponse

            Try

                Dim _appUserCtx As ID2LUserContext = _appContext.CreateUserContext(CreateOrRetrieveUserContextProperties())
                Dim _vAuth As New ValenceAuthenticator(_appUserCtx)
                Dim _rClient As RestSharp.RestClient = New RestSharp.RestClient(Authenticate.LMS_HOST_HTTPS)
                Dim _rRequest As RestSharp.RestRequest = New RestSharp.RestRequest(serviceToCall, RestMethod)

                _vAuth.Authenticate(_rClient, _rRequest)

                'Dim clien As Object = retorna(TypeObj)

                Select Case TypeObj.ToString()
                    Case "User"
                        Return _rClient.Execute(Of User.User)(_rRequest)
                    Case "UserProfile"
                        Return _rClient.Execute(Of User.UserProfile)(_rRequest)
                    Case "SchemaElement"
                        Return _rClient.Execute(Of Course.SchemaElement)(_rRequest)
                    Case "SchemaElements"
                        Return _rClient.Execute(Of Course.SchemaElements)(_rRequest)
                    Case "CourseOffering"
                        Return _rClient.Execute(Of Course.CourseOffering)(_rRequest)
                    Case "BasicOrgUnit"
                        Return _rClient.Execute(Of Course.BasicOrgUnit)(_rRequest)
                    Case "ClasslistUser"
                        Return _rClient.Execute(Of Enroll.ClasslistUser)(_rRequest)
                    Case "Organization"
                        Return _rClient.Execute(Of Org.Organization)(_rRequest)
                    Case "OrgInformation"
                        Return _rClient.Execute(Of Org.OrgInformation)(_rRequest)
                    Case "OrgUnitTypeInfo"
                        Return _rClient.Execute(Of OrgUnit.OrgUnitTypeInfo)(_rRequest)
                    Case "WhoAmIUser"
                        Return _rClient.Execute(Of User.WhoAmIUser)(_rRequest)
                    Case "Role"
                        Return _rClient.Execute(Of User.Role)(_rRequest)
                    Case "SocialMediaUrl"
                        Return _rClient.Execute(Of User.SocialMediaUrl)(_rRequest)
                    Case "Version"
                        Return _rClient.Execute(Of Version.Version)(_rRequest)
                    Case "GradeObject"
                        Return _rClient.Execute(Of Grade.GradeObject)(_rRequest)
                    Case "GradeObjectCategory"
                        Return _rClient.Execute(Of Grade.GradeObjectCategory)(_rRequest)
                    Case "GradeScheme"
                        Return _rClient.Execute(Of Grade.GradeScheme)(_rRequest)
                    Case "GradeSchemeRange"
                        Return _rClient.Execute(Of Grade.GradeSchemeRange)(_rRequest)
                    Case "GradeValue"
                        Return _rClient.Execute(Of Grade.GradeValue)(_rRequest)
                    Case "PagedResultSet"
                        Return _rClient.Execute(Of Api.PagedResultSet)(_rRequest)
                End Select

            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try

            Return Nothing

        End Function

        Public Function ExecuteService_with_AnonymousUserContext(ByVal serviceToCall As String, ByVal RestMethod As RestSharp.Method) As RestSharp.IRestResponse

            Dim _hs As HostSpec = New HostSpec(serviceToCall, Authenticate.LMS_HOST_HTTPS, Authenticate.LMS_PORT)
            Dim _appUserCtx As ID2LUserContext = _appContext.CreateAnonymousUserContext(_hs)
            Dim _vAuth As ValenceAuthenticator = New ValenceAuthenticator(_appUserCtx)
            Dim _rClient As RestSharp.RestClient = New RestSharp.RestClient(Authenticate.LMS_HOST_HTTPS)
            Dim _rRequest As RestSharp.RestRequest = New RestSharp.RestRequest(serviceToCall, RestMethod)

            _vAuth.Authenticate(_rClient, _rRequest)

            Return _rClient.Execute(_rRequest)

        End Function

#Region "Helper Methods"

        Private Function CreateOrRetrieveUserContextProperties() As UserContextProperties

            Return New UserContextProperties() _
            With {.HostName = Authenticate.LMS_HOST, .Port = Authenticate.LMS_PORT, .Scheme = Authenticate.SCHEME, .UserId = Authenticate.USER_ID, .UserKey = Authenticate.USER_KEY}

            'TODO: reuse a saved used properties

        End Function

        Private Function retorna(ByVal TypeObj As String) As Object

            Dim t As System.Type = System.Type.GetType("Teste.PoC.Valence.VB." + TypeObj.ToString())
            Return Activator.CreateInstance(t)

        End Function

#End Region

    End Class

End Namespace